package cn.iocoder.yudao.module.system.service.queryscene;

import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.mybatis.core.enums.QueryScenePage;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.module.ModuleService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.system.controller.admin.queryscene.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.queryscene.QuerySceneDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.queryscene.QuerySceneMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 高级查询场景 Service 实现类
 *
 * @author 数创万维
 */
@Service
@Validated
public class QuerySceneServiceImpl implements QuerySceneService {

    @Resource
    private QuerySceneMapper querySceneMapper;

    @Resource
    private AdminUserService adminUserService;

    @Override
    public Long createQueryScene(QuerySceneSaveReqVO createReqVO) {
        // 插入
        QuerySceneDO queryScene = BeanUtils.toBean(createReqVO, QuerySceneDO.class);
        queryScene.setOwnerUserId(getLoginUserId());
        querySceneMapper.insert(queryScene);
        // 返回
        return queryScene.getId();
    }

    @Override
    public void updateQueryScene(QuerySceneSaveReqVO updateReqVO) {
        // 更新
        QuerySceneDO updateObj = BeanUtils.toBean(updateReqVO, QuerySceneDO.class);
        querySceneMapper.updateById(updateObj);
    }

    @Override
    public void deleteQueryScene(Long id) {
        // 删除
        querySceneMapper.deleteById(id);
    }

    @Override
    public QuerySceneDO getQueryScene(Long id) {
        return querySceneMapper.selectById(id);
    }

    @Override
    public PageResult<QuerySceneDO> getQueryScenePage(QueryScenePageReqVO pageReqVO) {
        return querySceneMapper.selectPage(pageReqVO);
    }


    @Override
    public List<QuerySceneDO> getQuerySceneListByPage(String page) {
        return querySceneMapper.selectList(new LambdaQueryWrapperX<QuerySceneDO>().eq(QuerySceneDO::getPage, page).orderByDesc(QuerySceneDO::getSort));
    }

    @Override
    public List<QuerySceneBaseVO> getConditions(String page) {
        List<AdminUserDO> listByStatus = adminUserService.getUserListByStatus(CommonStatusEnum.ENABLE.getStatus());
        Map<Object, Object> longStringMap = CollectionUtils.convertMap(listByStatus, AdminUserDO::getId, AdminUserDO::getNickname);
        switch (page) {
            case QueryScenePage.CRM_CLUE:
                return CrmClueCondition.buildList(longStringMap);

            case QueryScenePage.CRM_CUSTOMER:
                return CrmCustomerCondition.buildList(longStringMap);

            case QueryScenePage.CALL_RECORD:
                return CallRecordCondition.buildList(longStringMap);
            default:
                throw exception(new ErrorCode(100500, "参数错误"));

        }
    }
}